﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Sublimation
{
    public class EnemyUnitWizard : EnemyUnit
    {
        new protected int NUMBER_OF_FRAMES_TO_PLAY = 4;
        new protected const int FRAMES_PER_SECOND = 10;
        protected float range =  200;
        protected Tower target;
        protected float rangeSpeed = 500;
        protected float moveTickMax = 20;
        protected float moveTick = 0;
        protected float rangedAttackTimerMillis = 0;

        public EnemyUnitWizard(WorldProperties worldProperties, EnemyKind kind)
            : base(worldProperties, kind)
        {
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            rangedAttackTimerMillis += gameTime.ElapsedGameTime.Milliseconds;

            getTarget();

            if (rangedAttackTimerMillis > meleeAttackDelay)
            {
                rangedAttackTimerMillis = 0;
                if (target != null)
                {
                    RangedAttack();
                }
                else
                {
                    attacking = false;
                }
            }
        }
        
        public override void DoMovement(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (target != null)
            {
                attacking = true;
            }
            else
            {
                base.DoMovement(gameTime);
            }
        }
        
        public void getTarget()
        {
            target = null;
            float minDistance = 0;
            float distance;
            foreach (Tower tower in worldProperties.Players[0].Towers)
            {
                distance = Vector2.Distance(tower.Position, position);
                if (distance < range)
                {
                    if (target == null || distance < minDistance)
                    {
                        target = tower;
                        minDistance = distance;
                    }
                }
            }

        }


        public void RangedAttack()
        {
            Vector2 bulletVelocity = target.Position - Position;
            bulletVelocity.Normalize();
            if (bulletVelocity.Y > 0)
            {
                Rotation = (float)Math.Acos(bulletVelocity.X);
            }
            else
            {
                Rotation = -(float)Math.Acos(bulletVelocity.X);
            }
            new IceBall(worldProperties, false, position, bulletVelocity, rangeSpeed, new Vector2(10, 10), range, meleeDamage);

        }

        protected override void LoadContent()
        {
            base.LoadContent();
            textureAnimator = new AnimatedSprite.AnimatedTexture(Origin, Rotation, Scale, 1.0f);
            textureAnimator.Load(Game.Content, "Sprites\\wizard", NUMBER_OF_FRAMES_TO_PLAY, FRAMES_PER_SECOND);
            textureAttack = new AnimatedSprite.AnimatedTexture(Origin, Rotation, Scale * 1f, 1.0f);
            textureAttack.Load(Game.Content, "Sprites\\wizard_attack", 6, 10);
            Scale = 1f;
        }

        public override void Kill()
        {
            base.Kill();
            if (Life <= 0) new Animation(worldProperties, this, 1f, 4, 10, "Sprites\\wizard_dying");
        }
    }
}
